//引入express模块
const express = require('express');
//创建商品路由器对象
const l = express.Router();
//引入数据库连接池模块
const laptopPool = require('../pool.js');

//1.添加按编号查看商品路由(get /get)
l.get('/get',(req,res,next)=>{
	//1.1在后台打印查询字符串传递的数据
	console.log(req.query);
	let obj=req.query;
	//1.2执行SQL命令
	//在aa_laptop_family中查询商品种类
	if(obj.name){
		laptopPool.query('SELECT * FROM aa_laptop_family WHERE name=?',[obj.name],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印查询的结果
			console.log(result);
			if(result.length>0){
				res.send({"code":200,"msg":"商品种类已存在","data":result});
			}else{
				res.send({"code":201,"msg":"商品种类不存在"});
			}
		});	
	}
	//在aa_laptop中查询商品详细信息
	if(obj.family_id){
		laptopPool.query('SELECT * FROM aa_laptop WHERE family_id=?',[obj.family_id],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印查询的结果
			console.log(result);
			if(result.length>0){
				res.send({"code":200,"msg":"商品已存在","data":result});
			}else{
				res.send({"code":201,"msg":"商品不存在"});
			}
		});
	}
	//在aa_laptop_pic中查询商品图片
	if(obj.laptop_id){
		laptopPool.query('SELECT * FROM aa_laptop_pic WHERE laptop_id=?',[obj.laptop_id],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印查询的结果
			console.log(result);
			if(result.length>0){
				res.send({"code":200,"msg":"商品图片已存在","data":result});
			}else{
				res.send({"code":201,"msg":"商品图片不存在"});
			}
		});		
	}

});


//2.添加商品添加路由 (post /reg)
l.post('/reg',(req,res,next)=>{
	//2.1在后台打印流传递的数据
	console.log(req.body);
	let obj=req.body;
	//2.2判断注册商品时的内容是否为空
	let n=400;
	for(let k in obj){
		if(!obj[k]){
			res.send({"code":n++,"msg":k+"不能为空"});
			return;
		}
	}
	//2.3执行SQL命令
	//2.3.1在aa_laptop_family中注册新商品
	if(obj.name){				
		laptopPool.query('INSERT INTO aa_laptop_family SET ?',[obj],( err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印结果
			console.log(result);
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"注册失败"}); 
			}else{
				res.send({"code":200,"msg":"注册成功"});
			}
		});	
	}
	//2.3.2在aa_laptop中加入关于新商品的信息
	if(obj.family_id){
		laptopPool.query('INSERT INTO aa_laptop SET ?',[obj],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印插入的商品信息
			console.log(result);
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"插入失败"});
			}else{
				res.send({"code":200,"msg":"插入成功"});
			}
		});	
	}
	//2.3.3在aa_laptop_pic中插入关于新商品的信息
	if(obj.laptop_id){
		laptopPool.query('INSERT INTO aa_laptop_pic SET ?',[obj],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印插入的商品的信息
			console.log(result);
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"图片插入失败"});
			}else{
				res.send({"code":200,"msg":"图片插入成功"});
			}
		});
	}
});

//3.添加商品删除路由('delete /:')
//3.1删除表aa_laptop_family中指定的商品('delete /a/:fid')
l.delete('/a/:fid',(req,res,next)=>{
	//在后台打印路由传参传递的数据
	console.log(req.params);
	let obj=req.params;
	//执行SQL命令	
	laptopPool.query('DELETE FROM aa_laptop_family WHERE fid=?',[obj.fid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//打印result并查看affectedRows是否为0，为0则删除失败，否则删除成功
		console.log(result);
		if(result.affectedRows===0){
				res.send({"code":201,"msg":"删除失败"});
		}else{
			res.send({"code":200,"msg":"删除成功"});
		}
	});
});
//3.2删除表aa_laptop中指定的商品('delete /b/:lid')
l.delete('/b/:lid',(req,res,next)=>{
	//在后台打印路由传参传递的数据
	console.log(req.params);
	let obj=req.params;
	//执行SQL命令	
	laptopPool.query('DELETE FROM aa_laptop WHERE lid=?',[obj.lid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//打印result并查看affectedRows是否为0，为0则删除失败，否则删除成功
		console.log(result);
		if(result.affectedRows===0){
			res.send({"code":201,"msg":"删除失败"});
		}else{
			res.send({"code":200,"msg":"删除成功"});
		}
	});	
});
//3.3删除表aa_laptop_pic中指定的商品('delete /c/:pid')
l.delete('/c/:pid',(req,res,next)=>{
	//在后台打印路由传参传递的数据
	console.log(req.params);
	let obj=req.params;
	//执行SQL命令	
	laptopPool.query('DELETE FROM aa_laptop_pic WHERE pid=?',[obj.pid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//打印result并查看affectedRows是否为0，为0则删除失败，否则删除成功
		console.log(result);
		if(result.affectedRows===0){
				res.send({"code":201,"msg":"删除失败"});
		}else{
			res.send({"code":200,"msg":"删除成功"});
		}
	});	
});

//4.添加修改商品路由(put /)
l.put('/',(req,res,next)=>{
	//4.1在控制台打印流传递的数据
	console.log(req.body);
	let obj=req.body;
	//4.2判断用户修改的值是否为空，为空则跳出并报错
	let n=400;
	for(let k in obj){
		if(!obj[k]){
			res.send({"code":n++,"msg":k+"不能为空"});
			return;
		}
	}
	//4.3修改aa_laptop_family中的商品种类信息
	if(obj.fid){
		//执行SQL命令
		laptopPool.query('UPDATE aa_laptop_family SET ? WHERE fid=?',[obj,obj.fid],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			console.log(result);
			//在控制台打印result并判断affectedRows是否为0，为0则修改失败，否则修改成功
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"修改失败"});
			}else{
				res.send({"code":200,"msg":"修改成功"});
			}
		});	
	}
	//4.4修改aa_laptop中的商品信息
	if(obj.lid){
		//执行SQL命令
		laptopPool.query('UPDATE aa_laptop SET ? WHERE lid=?',[obj,obj.lid],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			console.log(result);
			//在控制台打印result并判断affectedRows是否为0，为0则修改失败，否则修改成功
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"修改失败"});
			}else{
				res.send({"code":200,"msg":"修改成功"});
			}
		});	
	}
	//4.5修改aa_laptop_pic中的商品信息
	if(obj.pid){
		//执行SQL命令
		laptopPool.query('UPDATE aa_laptop_pic SET ? WHERE pid=?',[obj,obj.pid],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			////在控制台打印result并判断affectedRows是否为0，为0则修改失败，否则修改成功
			console.log(result);
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"修改失败"});
			}else{
				res.send({"code":200,"msg":"修改成功"});
			}
		});
	}
});

//5.添加查询商品路由(get /list)
//5.1查询商品种类(get /list1)
l.get('/list1',(req,res,next)=>{
	//5.1获取查询字符串传递的数据
	console.log(req.query);
	let obj=req.query;
	//5.2查询商品列表
	if(obj.pno || obj.count){
		//判断
		if(!obj.pno) obj.pno=1;
		if(!obj.count) obj.count=5;
		//计算每页开始查询的值
		let start=(obj.pno-1)*obj.count;
		//将每页的数据量转为整形
		let size=parseInt(obj.count);
		//执行SQL命令
		laptopPool.query('SELECT * FROM aa_laptop_family LIMIT ?,?',[start,size],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印查询的结果
			console.log(result);
			if(result.length>0){
				res.send({"code":200,"msg":"查询成功","data":result});
			}else{
				res.send({"code":201,"msg":"查询失败","data":result});
			}
		});
	}
});
//5.2查询商品详细信息(get /list2)
l.get('/list2',(req,res,next)=>{
	//5.1获取查询字符串传递的数据
	console.log(req.query);
	let obj=req.query;
	//5.2查询商品列表
	if(obj.pno || obj.count){
		//判断
		if(!obj.pno) obj.pno=1;
		if(!obj.count) obj.count=5;
		//计算每页开始查询的值
		let start=(obj.pno-1)*obj.count;
		//将每页的数据量转为整形
		let size=parseInt(obj.count);
		//执行SQL命令
		laptopPool.query('SELECT * FROM aa_laptop LIMIT ?,?',[start,size],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印查询的结果
			console.log(result);
			if(result.length>0){
				res.send({"code":200,"msg":"查询成功","data":result});
			}else{
				res.send({"code":201,"msg":"查询失败","data":result});
			}
		});
	}
});
//5.3查询商品图片信息(get /list3)
l.get('/list3',(req,res,next)=>{
	//5.1获取查询字符串传递的数据
	console.log(req.query);
	let obj=req.query;
	//5.2查询商品列表
	if(obj.pno || obj.count){
		//判断
		if(!obj.pno) obj.pno=1;
		if(!obj.count) obj.count=5;
		//计算每页开始查询的值
		let start=(obj.pno-1)*obj.count;
		//将每页的数据量转为整形
		let size=parseInt(obj.count);
		//执行SQL命令
		laptopPool.query('SELECT * FROM aa_laptop_pic LIMIT ?,?',[start,size],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印查询的结果
			console.log(result);
			if(result.length>0){
				res.send({"code":200,"msg":"查询成功","data":result});
			}else{
				res.send({"code":201,"msg":"查询失败","data":result});
			}
		});
	}
});

//6.添加随机查询商品路由(get /sui) 分三个路由 /sui1 /sui2 /sui3
//6.1查询商品种类路由(get /sui1)
l.get('/sui1',(req,res,next)=>{
	//接受查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//执行SQL命令
	laptopPool.query('SELECT * FROM aa_laptop_family WHERE ?',[obj],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询商品种类成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询商品种类失败","data":result});
		}
	});
});
//6.2查询商品路由(get /sui2)
l.get('/sui2',(req,res,next)=>{
	//接受查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//执行SQL命令
	laptopPool.query('SELECT * FROM aa_laptop WHERE ?',[obj],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询商品成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询商品失败","data":result});
		}
	});
});
//6.3查询商品图片路由(get /sui3)
l.get('/sui3',(req,res,next)=>{
	//接受查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//执行SQL命令
	laptopPool.query('SELECT * FROM aa_laptop_pic WHERE ?',[obj],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询商品图片成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询商品图片失败","data":result});
		}
	});
});

//7.添加获取所有商品信息(get /laptoplist)
//7.1获取商品种类信息(get /laptoplist1)
l.get('/laptoplist1',(req,res,next)=>{
	//获取查询字符串传递的数据
	console.log(req.query);
	//执行SQL命令
	laptopPool.query('SELECT * FROM aa_laptop_family',(err,result)=>{
		if(err){
			//出现错误，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"查询失败","data":result});
		}else{
			res.send({"code":200,"msg":"查询成功","data":result});
		}
	});
});
//7.2获取商品信息(get /laptoplist2)
l.get('/laptoplist2',(req,res,next)=>{
	//获取查询字符串传递的数据
	console.log(req.query);
	//执行SQL命令
	laptopPool.query('SELECT * FROM aa_laptop',(err,result)=>{
		if(err){
			//出现错误，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"查询失败"});
		}else{
			res.send({"code":200,"msg":"查询成功","data":result});
		}
	});
});
//7.3获取商品图片信息(get /laptoplist3)
l.get('/laptoplist3',(req,res,next)=>{
	//获取查询字符串传递的数据
	console.log(req.query);
	//执行SQL命令
	laptopPool.query('SELECT * FROM aa_laptop_pic',(err,result)=>{
		if(err){
			//出现错误，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"查询失败"});
		}else{
			res.send({"code":200,"msg":"查询成功","data":result});
		}
	});
});
//导出路由器对象
module.exports = l;